
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace CollapsableControl
{
	/// <summary>
	/// Summary description for CollapsablePanel.
	/// </summary>
	 public partial class CollapsablePanel : System.Windows.Forms.UserControl
	{
		public System.Windows.Forms.Button button1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ImageList imageList1;
		private System.ComponentModel.IContainer components;
		private int height = 120;
		private int AnimationRate = 100;
        private string title = string.Empty;
        private bool _bIsCollapsed = true; //mani made this true from false;

               
        /// <summary>
		/// Gets/Sets Panel Label
		/// </summary>
		public string Button_Text
		{ 
			get
			{ 
				return this.button1.Text.Trim(); 
			}
			set
			{
				this.button1.Text = "          "+value;
			}
		}

        /// <summary>
        /// Gets/Sets the title for the pane
        /// </summary>
        public string Title
        {
            get
            {
                return this.title;
            }
            set
            {
                this.title = value;
            }
        }

		/// <summary>
		/// Gets/Sets Contentpane Height
		/// </summary>
		public int ContentPane_HEIGHT
		{ 
			get
			{ 
				return this.height; 
			}
			set
			{
				this.height = value;
			}
		}

		
		/// <summary>
		/// Gets/Sets Animation Rate
		/// </summary>
		public int Animation_Rate
		{ 
			get
			{ 
				return this.AnimationRate;
			}
			set
			{
				this.AnimationRate = value;
			}
		}

		/// <summary>
		/// Gets/Sets Content Pane - Any Form control can be dragged into this Content Pane.
		/// </summary>
		public System.Windows.Forms.Panel ContentPane
		{ 
			get
			{
				try
				{
					return (Panel)this.panel1.Controls[0];
				}
				catch(Exception e)
				{
					e=e;
				}
				return null;
			}
			set
			{
				try
				{
					this.panel1.Controls.Clear();
					this.panel1.Controls.Add(value);
				
				}
				catch(Exception e)
				{
					e=e;
				}
			}
		}

		public CollapsablePanel()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call            
        }
        
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CollapsablePanel));
            this.button1 = new System.Windows.Forms.Button();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
            this.button1.Dock = System.Windows.Forms.DockStyle.Top;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.button1.ImageIndex = 1;
            this.button1.ImageList = this.imageList1;
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(214, 23);
            this.button1.TabIndex = 0;
            this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "Icon.bmp");
            this.imageList1.Images.SetKeyName(1, "Icon.bmp");
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.panel1.Location = new System.Drawing.Point(0, 23);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(214, 133);
            this.panel1.TabIndex = 1;
            // 
            // CollapsablePanel
            // 
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Name = "CollapsablePanel";
            this.Size = new System.Drawing.Size(214, 156);
            this.Load += new System.EventHandler(this.CollapsablePanel_Load);
            this.Resize += new System.EventHandler(this.CollapsablePanel_Resize);
            this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
            if (this._bIsCollapsed)
                ExpandThisPane();
            //else
            //    CollapsePane(this);

            //if(this.Height > this.button1.Height)
            //{
            //                CollapsePane(this);
            //			}
            //else if (this.Height == this.button1.Height)
            //{
            //    ExpandThisPane();
            //}            			
		}

        /// <summary>
        /// Collapses the given pane and relocates other panes which follows it.
        /// </summary>
        /// <param name="pane"></param>
        public void CollapsePane(CollapsablePanel pane)
        {
            while (pane.Height > pane.button1.Height)
            {
                Application.DoEvents();
                pane.Height -= AnimationRate;
            }
            pane.button1.ImageIndex = 1;
            pane.Height = pane.button1.Height;
            RelocateOtherPanesAfterCollapse(pane);
            pane._bIsCollapsed = true;
        }
        public bool IsCollapsed()
        {
            return _bIsCollapsed;

        }
        /// <summary>
        /// Relocates other panes relative to the collapsed pane. 
        /// Logic - It iterates through the control children of the parent Control(form) and sets relative positions.
        /// </summary>
        /// <param name="pane"></param>
        private void RelocateOtherPanesAfterCollapse(CollapsablePanel pane)
        {
            Control parentControl = pane.Parent;
            if (this.Parent == null)
                return;
            CollapsablePanel previousChildControl = pane;
            while (true)
            {
                CollapsablePanel nextChildControl = (CollapsablePanel)parentControl.GetNextControl(previousChildControl, true);
                if (null != nextChildControl)
                {
                    nextChildControl.Location = new System.Drawing.Point(previousChildControl.Location.X, previousChildControl.Location.Y + previousChildControl.button1.Height);// + 10);Mani
                }
                else
                {
                    break;
                }
                previousChildControl = nextChildControl;
            }            
        }

        /// <summary>
        /// Expand the current pane.
        /// Step1 - Closes all other expanded panes so that only one is open at a time.
        /// Step2 - Expands this pane
        /// Step3 - Relocates the following panes.
        /// </summary>
        public void ExpandThisPane()
        {
            CloseOtherExpandedPanes();
            int x = this.button1.Height + this.height;
            while (this.Height <= (x))
            {
                Application.DoEvents();                			
                this.Height += AnimationRate;
            }
            this.button1.ImageIndex = 0;
            this.Height = x;
            RelocateOtherPanesAfterExpansion();
            this._bIsCollapsed = false;
        }

        /// <summary>
        /// Checks if any other pane is expanded by iterating through the parent control.
        /// If so, it collapses those panes
        /// Called by ExpandThisPane().
        /// </summary>
        private void CloseOtherExpandedPanes()
        {
            if (this.Parent == null)
                return;
            Control parentControl = this.Parent;
            foreach (Control childControl in parentControl.Controls)
            {
                if (((CollapsablePanel)childControl)._bIsCollapsed == false)
                {
                    CollapsePane((CollapsablePanel)childControl);
                }
            }
        }

        /// <summary>
        /// Sets relative positions of the following panes after expansion.
        /// For the first time the relative position is the size of the expanded pane
        /// For the rest, it's set realtive to the collapsed control size.
        /// </summary>
        private void RelocateOtherPanesAfterExpansion()
        {
            if (this.Parent == null)
                return;

            Control parentControl = this.Parent;
            CollapsablePanel previousChildControl = this;
            bool bFirstTime = true;
            while (true)
            {
                CollapsablePanel nextChildControl = (CollapsablePanel)parentControl.GetNextControl(previousChildControl, true);
                if (null != nextChildControl)
                {
                    if (bFirstTime == true)
                    {
                        nextChildControl.Location = new System.Drawing.Point(previousChildControl.Location.X, previousChildControl.Location.Y + previousChildControl.button1.Height + previousChildControl.height);// + 10);Mani
                        bFirstTime = false;
                    }
                    else
                    {
                        nextChildControl.Location = new System.Drawing.Point(previousChildControl.Location.X, previousChildControl.Location.Y + previousChildControl.button1.Height);// + 10);Mani
                    }
                }
                else
                {
                    break;
                }
                previousChildControl = nextChildControl;
            }
        }

		private void CollapsablePanel_Resize(object sender, System.EventArgs e)
		{
			if((this.Height%2)!= 0)
				this.Height += 1;
		}

		private void CollapsablePanel_Load(object sender, System.EventArgs e)
		{
			//this.Height = this.height + this.button1.Height;
		}
	}
}
